<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="采集时间" prop="acquireTime">
        <el-date-picker clearable
          v-model="queryParams.acquireTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择采集时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceId">
        <el-select v-model="queryParams.deviceId" placeholder="请选择设备号" clearable>
          <el-option
            v-for="device in deviceList"
            :key="device.deviceId"
            :label="device.deviceId"
            :value="device.deviceId"
          />
          </el-select>
      </el-form-item>
      <el-form-item label="空气温度" prop="airTemp">
        <el-input
          v-model="queryParams.airTemp"
          placeholder="请输入空气温度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="空气湿度" prop="airHump">
        <el-input
          v-model="queryParams.airHump"
          placeholder="请输入空气湿度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="光合有效辐射" prop="sunPar">
        <el-input
          v-model="queryParams.sunPar"
          placeholder="请输入光合有效辐射"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="风速" prop="windSpeed">
        <el-input
          v-model="queryParams.windSpeed"
          placeholder="请输入风速"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="风向" prop="windDirection">
        <el-input
          v-model="queryParams.windDirection"
          placeholder="请输入风向"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="降雨量" prop="rainPrcp">
        <el-input
          v-model="queryParams.rainPrcp"
          placeholder="请输入降雨量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="土壤温度" prop="soilTemp">
        <el-input
          v-model="queryParams.soilTemp"
          placeholder="请输入土壤温度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="土壤湿度" prop="soilHump">
        <el-input
          v-model="queryParams.soilHump"
          placeholder="请输入土壤湿度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="pH值" prop="soilPh">
        <el-input
          v-model="queryParams.soilPh"
          placeholder="请输入pH值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="土壤电导率" prop="soilEc">
        <el-input
          v-model="queryParams.soilEc"
          placeholder="请输入土壤电导率"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据传输单元" prop="dtu">
        <el-input
          v-model="queryParams.dtu"
          placeholder="请输入数据传输单元"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['grape:iot:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['grape:iot:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['grape:iot:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['grape:iot:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="iotList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="meteId" />
      <el-table-column label="采集时间" align="center" prop="acquireTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.acquireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备编号" align="center" prop="deviceId" />
      <el-table-column label="空气温度" align="center" prop="airTemp" />
      <el-table-column label="空气湿度" align="center" prop="airHump" />
      <el-table-column label="光合有效辐射" align="center" prop="sunPar" />
      <el-table-column label="风速" align="center" prop="windSpeed" />
      <el-table-column label="风向" align="center" prop="windDirection" />
      <el-table-column label="降雨量" align="center" prop="rainPrcp" />
      <el-table-column label="土壤温度" align="center" prop="soilTemp" />
      <el-table-column label="土壤湿度" align="center" prop="soilHump" />
      <el-table-column label="pH值" align="center" prop="soilPh" />
      <el-table-column label="土壤电导率" align="center" prop="soilEc" />
      <el-table-column label="数据传输单元" align="center" prop="dtu" />
      <el-table-column label="备注" align="center" prop="meteNote" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['grape:iot:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['grape:iot:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物联网监测数据对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="iotRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采集时间" prop="acquireTime">
          <el-date-picker clearable
            v-model="form.acquireTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择采集时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceId">
          <el-select v-model="form.deviceId" placeholder="请选择设备号" clearable>
          <el-option
            v-for="device in deviceList"
            :key="device.deviceId"
            :label="device.deviceId"
            :value="device.deviceId"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="空气温度" prop="airTemp">
          <el-input v-model="form.airTemp" placeholder="请输入空气温度" />
        </el-form-item>
        <el-form-item label="空气湿度" prop="airHump">
          <el-input v-model="form.airHump" placeholder="请输入空气湿度" />
        </el-form-item>
        <el-form-item label="光合有效辐射" prop="sunPar">
          <el-input v-model="form.sunPar" placeholder="请输入光合有效辐射" />
        </el-form-item>
        <el-form-item label="风速" prop="windSpeed">
          <el-input v-model="form.windSpeed" placeholder="请输入风速" />
        </el-form-item>
        <el-form-item label="风向" prop="windDirection">
          <el-input v-model="form.windDirection" placeholder="请输入风向" />
        </el-form-item>
        <el-form-item label="降雨量" prop="rainPrcp">
          <el-input v-model="form.rainPrcp" placeholder="请输入降雨量" />
        </el-form-item>
        <el-form-item label="土壤温度" prop="soilTemp">
          <el-input v-model="form.soilTemp" placeholder="请输入土壤温度" />
        </el-form-item>
        <el-form-item label="土壤湿度" prop="soilHump">
          <el-input v-model="form.soilHump" placeholder="请输入土壤湿度" />
        </el-form-item>
        <el-form-item label="pH值" prop="soilPh">
          <el-input v-model="form.soilPh" placeholder="请输入pH值" />
        </el-form-item>
        <el-form-item label="土壤电导率" prop="soilEc">
          <el-input v-model="form.soilEc" placeholder="请输入土壤电导率" />
        </el-form-item>
        <el-form-item label="数据传输单元" prop="dtu">
          <el-input v-model="form.dtu" placeholder="请输入数据传输单元" />
        </el-form-item>
        <el-form-item label="备注" prop="meteNote">
          <el-input v-model="form.meteNote" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Iot">
import { listIot, getIot, delIot, addIot, updateIot } from "@/api/grape/iot";

const { proxy } = getCurrentInstance();

const iotList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    acquireTime: null,
    deviceId: null,
    airTemp: null,
    airHump: null,
    sunPar: null,
    windSpeed: null,
    windDirection: null,
    rainPrcp: null,
    soilTemp: null,
    soilHump: null,
    soilPh: null,
    soilEc: null,
    dtu: null,
    meteNote: null
  },
  rules: {
    acquireTime: [
      { required: true, message: "采集时间不能为空", trigger: "blur" }
    ],
    deviceId: [
      { required: true, message: "设备编号不能为空", trigger: "blur" }
    ],
    airTemp: [
      { required: true, message: "空气温度不能为空", trigger: "blur" }
    ],
    airHump: [
      { required: true, message: "空气湿度不能为空", trigger: "blur" }
    ],
    sunPar: [
      { required: true, message: "光合有效辐射不能为空", trigger: "blur" }
    ],
    windSpeed: [
      { required: true, message: "风速不能为空", trigger: "blur" }
    ],
    windDirection: [
      { required: true, message: "风向不能为空", trigger: "blur" }
    ],
    rainPrcp: [
      { required: true, message: "降雨量不能为空", trigger: "blur" }
    ],
    soilTemp: [
      { required: true, message: "土壤温度不能为空", trigger: "blur" }
    ],
    soilHump: [
      { required: true, message: "土壤湿度不能为空", trigger: "blur" }
    ],
    soilPh: [
      { required: true, message: "pH值不能为空", trigger: "blur" }
    ],
    soilEc: [
      { required: true, message: "土壤电导率不能为空", trigger: "blur" }
    ],
    dtu: [
      { required: true, message: "数据传输单元不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询物联网监测数据列表 */
function getList() {
  loading.value = true;
  listIot(queryParams.value).then(response => {
    iotList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 查询设备基本信息列表,外键 */
import {listDevice} from "@/api/grape/device";
const deviceList = ref([]);
function getDeviceList() {
  listDevice("").then(response => {
    deviceList.value = response.rows;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    meteId: null,
    acquireTime: null,
    deviceId: null,
    airTemp: null,
    airHump: null,
    sunPar: null,
    windSpeed: null,
    windDirection: null,
    rainPrcp: null,
    soilTemp: null,
    soilHump: null,
    soilPh: null,
    soilEc: null,
    dtu: null,
    meteNote: null
  };
  proxy.resetForm("iotRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.meteId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加物联网监测数据";
  getDeviceList();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _meteId = row.meteId || ids.value
  getIot(_meteId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改物联网监测数据";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["iotRef"].validate(valid => {
    if (valid) {
      if (form.value.meteId != null) {
        updateIot(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addIot(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _meteIds = row.meteId || ids.value;
  proxy.$modal.confirm('是否确认删除物联网监测数据编号为"' + _meteIds + '"的数据项？').then(function() {
    return delIot(_meteIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('grape/iot/export', {
    ...queryParams.value
  }, `iot_${new Date().getTime()}.xlsx`)
}

getList();
getDeviceList();
</script>
